package br.com.ac.fatureweb.service;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import br.com.ac.fatureweb.integration.enums.EnumStatus;
import br.com.ac.fatureweb.model.Usuario;
import br.com.ac.fatureweb.utilitary.Criptografia;
import br.com.ac.fatureweb.utilitary.NgcDefaultBean;

@Local
@Named
@Stateless
public class NgcUsuarioBean extends NgcDefaultBean<Usuario> {

	@PersistenceContext
	private EntityManager entityManager;

	@PostConstruct
	void inicialize() {
		setEm(entityManager);
	}

	/*
	 * @see
	 * br.com.ac.fatureweb.utilitary.NgcDefaultBean#persist(java.lang.Object)
	 */
	@Override
	public Usuario persist(Usuario entity) {
		entity.setStatus(EnumStatus.ATIVO);

		entity.setSenha(Criptografia.executeSha(entity.getSenha()));

		return super.persist(entity);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 31/03/2015
	 *
	 * @see br.com.ac.fatureweb.utilitary.NgcDefaultBean#merge(java.lang.Object)
	 */
	@Override
	public Usuario merge(Usuario entity) {

		entity.setSenha(Criptografia.executeSha(entity.getSenha()));

		return super.merge(entity);
	}

}
